package com.zxjbyte.yiyi.framework.common.core.pdf.event;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.BooleanUtil;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfGState;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;

/**
 * 简单图片水印, 默认顺时针旋转45度
 * @Author zhangxingjia
 * @Date 2023/3/24 16:40
 * @Version: 1.0
 */
public class SimpleImageWatermark extends PdfPageEventHelper {

    // 图片路径
    private final String imagePath;
    // 是否自动等比例缩放
    private final Boolean isAutoScale;
    // 图片旋转角度，正数顺时针，负数逆时针, 0不旋转
    private final float rotation;

    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        try {

            Image image = Image.getInstance(FileUtil.getAbsolutePath(imagePath));
            PdfContentByte canvas = writer.getDirectContent();
            // 设置图形状态的填充透明度
            PdfGState gs = new PdfGState();
            gs.setFillOpacity(0.3f);
            canvas.setGState(gs);
            // 设置水印图片本身的透明度
//            image.setTransparency(new int[]{0x00, 0x40});
            // 获取页面大小
            Rectangle pageSize = document.getPageSize();
            // 基于文档宽度和高度计算
            float pageWidth = pageSize.getWidth();
            float pageHeight = pageSize.getHeight();

            // 获取图片大小
            float imageWidth = image.getWidth();
            float imageHeight = image.getHeight();

            // 计算缩放比例
            if(BooleanUtil.isTrue(isAutoScale)){
                float widthRatio = pageWidth / imageWidth;
                float heightRatio = pageHeight / imageHeight;
                float ratio = Math.min(widthRatio, heightRatio);
                image.scalePercent(ratio * 100);
            }
            // 双线性插值算法，可以提高缩放图片质量，比较占用内存
//            image.setInterpolation(true);
            // 将图片顺时针旋转45度 （如果要逆时针去掉减号即可） 参数是以弧度为单位的浮点数
            image.setRotation(rotation);
            // 将图片顺时针旋转45度 参数是以度为单位的浮点数
//            image.setRotationDegrees(-45);
            // 将图片放置在 PDF 页面的中央位置 （注意缩放居中计算要在旋转角度之后，不然位置不准确）
            float x = (pageWidth - image.getScaledWidth()) / 2;
            float y = (pageHeight - image.getScaledHeight()) / 2;
            image.setAbsolutePosition(x, y);
            canvas.addImage(image);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public SimpleImageWatermark(String imagePath){
        this(imagePath, Boolean.TRUE, 45f);
    }

    public SimpleImageWatermark(String imagePath, Boolean isAutoScale){
        this(imagePath, isAutoScale, 45f);
    }

    public SimpleImageWatermark(String imagePath, float rotation){
        this(imagePath, Boolean.TRUE, rotation);
    }

    public SimpleImageWatermark(String imagePath, Boolean isAutoScale, float rotation){
        this.imagePath = imagePath;
        this.isAutoScale = isAutoScale;
        this.rotation = rotation;
    }
}
